package com.cgbl.cg.ui.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityLimitedTimeBinding
import com.cgbl.cg.ui.course.CourseMenuFragment
import com.cgbl.cg.vm.home.LimitedTimeViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants

@Route(path = AppConstants.Router.HOME.A_LIMITED_TIME)
class LimitedTimeActivity: GLBaseActivity<AppActivityLimitedTimeBinding, LimitedTimeViewModel>() {
    private var fragmentList = mutableListOf<Fragment>()

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_limited_time

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_en_tips_46))
        fragmentList.add(LimitedTimeFragment.newInstance("0"))
        fragmentList.add(LimitedTimeFragment.newInstance("0"))
        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.mViewPager2.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                when(position){
                    0->{
                        viewModel.leftCommand.execute()
                    }
                    else->{
                        viewModel.rightCommand.execute()
                    }
                }
            }

        })
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.tabTypeEvent.observe(this){
            if(it){
                binding.mViewPager2.currentItem = 0
            }else{
                binding.mViewPager2.currentItem = 1
            }
        }
    }
}